﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace YXShop.Web.admin.product
{
    public partial class product_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("001008001");
                if (ChangeHope.WebPage.PageRequest.GetFormString("Option") != string.Empty && ChangeHope.WebPage.PageRequest.GetFormString("id") != string.Empty)
                {
                    
                    string types = Request.Form["Option"].Trim();
                    string StrID = ChangeHope.WebPage.PageRequest.GetFormString("id");
                    if (types == "del")
                    {

                       if(YXShop.Common.PromptInfo.Message("001008003")!="ok")
                       {
                        del(StrID);
                       }
                        else
                       {
                        Response.Write ("no");
                       }
                    }
                    Response.End();
                    return;
                }
                string PutoutType = ChangeHope.WebPage.PageRequest.GetString("q_PutoutType");
                if (PutoutType != "")
                {
                    if (PutoutType == "0")
                    {
                        this.OptionMember.Visible = false;
                    }
                    this.returnLinkBottom.NavigateUrl = "product_info_edit.aspx?putoutType=" + PutoutType;
                    this.w_d_pro_PutoutType.Value = PutoutType;
                    GetList(PutoutType);
                }
                BindProductBrand();
                InitWebControl();
            }
        }

        #region 搜索设置
        private void InitWebControl()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            this.txtArea.Attributes.Add("readonly", "readonly");
            this.txtArea.Attributes.Add("onclick", "selectFile('Area',new Array(" + this.w_l_pro_Area.ClientID + "," + this.txtArea.ClientID + "),310,450,'" + sp.DummyPaht + "');");
            this.txtProductClass.Attributes.Add("readonly", "readonly");
            this.txtProductClass.Attributes.Add("onclick", "selectFile('Productclass',new Array(" + this.w_d_pro_CID.ClientID + "," + this.txtProductClass.ClientID + "),310,450,'" + sp.DummyPaht + "');");
            this.txtUserName.Attributes.Add("readonly", "readonly");
            this.txtUserName.Attributes.Add("onclick", "selectFile('Memberlist',new Array(" + this.w_d_pro_PutoutID.ClientID + "," + this.txtUserName.ClientID + "),310,450,'" + sp.DummyPaht + "');");
        }
        #endregion

        #region 商品品牌
        protected void BindProductBrand()
        {
            YXShop.BLL.Product.ProductBrand bll = new YXShop.BLL.Product.ProductBrand();
            ChangeHope.DataBase.DataByPage dataPage = bll.GetList();
            this.w_d_pro_BrandID.Items.Add(new ListItem("--所有品牌--", ""));
            if (dataPage.DataReader != null)
            {
                while (dataPage.DataReader.Read())
                {
                    this.w_d_pro_BrandID.Items.Add(new ListItem(dataPage.DataReader["name"].ToString(), dataPage.DataReader["bid"].ToString()));
                }
            }
        }
        #endregion
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        protected void GetList(string PutoutType)
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.BLL.Product.ProductInfo data = new YXShop.BLL.Product.ProductInfo();
            ChangeHope.DataBase.DataByPage dataPage = data.GetList("[order by] pro_ID desc", 20, " and pro_PutoutType=" + PutoutType);
            //第一步先添加表头
            table.AddHeadCol("7%", "<input type=\"checkbox\" id=\"chkAll\" onclick=\"CheckAll(this.form)\" alt=\"全选/取消\" />选择");
            table.AddHeadCol("20%", "所属分类");
            table.AddHeadCol("25%", "商品名称");
            table.AddHeadCol("10%", "商城价");
            table.AddHeadCol("8%", "库存");
            table.AddHeadCol("15%", "状态");
            table.AddHeadCol("15%", "操作");
            table.AddRow();
            //添加表的内容
            if (dataPage.DataReader != null)
            {
                while (dataPage.DataReader.Read())
                {
                    table.AddCol("<input ID=\"cBox\" type=\"checkbox\" value=\"" + dataPage.DataReader["pro_ID"].ToString() + "\" />");
                    table.AddCol(ProductClassName(dataPage.DataReader["cid"].ToString()));
                    string pic_url = sp.DummyPaht + dataPage.DataReader["pro_Thumbnail"].ToString();
                    if (dataPage.DataReader["pro_Thumbnail"].ToString().IndexOf("http://") > -1)
                    {
                        pic_url = dataPage.DataReader["pro_Thumbnail"].ToString();
                    }

                    table.AddCol("<span style='cursor:hand'  onMouseOut=\"hiddenPic();\" onMouseMove=\"showPic('" + pic_url + "');\">" + dataPage.DataReader["pro_Name"].ToString() + "</span>");
                    double price = Convert.ToDouble(dataPage.DataReader["pro_ShopPrice"].ToString());
                    int productStock = Convert.ToInt32(dataPage.DataReader["pro_Stock"].ToString());
                    if (!string.IsNullOrEmpty(dataPage.DataReader["pro_Specifications"].ToString()))
                    {
                        YXShop.BLL.Product.ProductSpecification proSpe = new YXShop.BLL.Product.ProductSpecification();
                        List<YXShop.Model.Product.ProductSpecification> proSpeList = proSpe.GetSpecification(Convert.ToInt32(dataPage.DataReader["pro_ID"].ToString()));
                        if (proSpeList.Count > 0)
                        {
                            price = Convert.ToDouble(proSpeList[0].SalePrice);
                            for (int i = 0; i < proSpeList.Count; i++)
                            {
                                productStock += proSpeList[i].ProductStock;
                            }
                        }
                    }
                    table.AddCol("￥" + price.ToString("f2"));
                    table.AddCol(string.Format("{0}{1}", productStock.ToString(), dataPage.DataReader["pro_Unit"].ToString()));
                    table.AddCol(string.Format("{0}<br>{1}", States(dataPage.DataReader["pro_State"].ToString()), Designation(dataPage.DataReader["pro_Designation"].ToString())));
                    string special = "<span style='color:#ccc' title='特殊规格'>特</span>";
                    if (!string.IsNullOrEmpty(dataPage.DataReader["pro_Specialspecifications"].ToString()))
                    {
                        special = "<a href='product_specialspecification_list.aspx?id=" + dataPage.DataReader["pro_ID"].ToString() + "&putoutType=" + PutoutType + "' title='特殊规格'>特</a>";
                    }
                    table.AddCol(string.Format("<a href=product_info_edit.aspx?cid={1}&id={0}&putoutType={2}>编辑</a> <a href='#' onclick='Del({0})'>删除</a> " + special + " <a href='../../product/productcontent.aspx?q_productid={0}' target='_blank'>浏览</a>", dataPage.DataReader["pro_ID"].ToString(), dataPage.DataReader["cid"].ToString(), PutoutType));

                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            this.Literal1.Text = view;
        }
        /// <summary>
        /// 获取分类
        /// </summary>
        /// <param name="strId"></param>
        /// <returns></returns>
        protected string ProductClassName(string strId)
        {
            string reStr = string.Empty;
            string str = "暂无归类";
            if (!string.IsNullOrEmpty(strId))
            {
                YXShop.BLL.Product.Productclass dll = new YXShop.BLL.Product.Productclass();
                DataTable dt = dll.GetMoreThanClassName(strId);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (!string.IsNullOrEmpty(reStr))
                    {
                        reStr = reStr + "," + dt.Rows[i]["name"].ToString();
                    }
                    else
                    {
                        reStr = dt.Rows[i]["name"].ToString();
                    }
                }
                if (!string.IsNullOrEmpty(reStr))
                {
                    str = reStr;
                }
            }
            return str;
        }
        #region 状态
        protected string States(string str)
        {
            string reStr = string.Empty;
            string[] value = str.Split('|');
            for (int i = 0; i < value.Length; i++)
            {
                switch (i)
                {
                    case 0:
                        if (value[i].ToString() == "1")
                        {
                            reStr += "&nbsp;<span style='color:red'>推</span>";
                        }
                        else
                        {
                            reStr += "&nbsp;推";
                        }
                        break;
                    case 1:
                        if (value[i].ToString() == "1")
                        {
                            reStr += "&nbsp;<span style='color:red'>热</span>";
                        }
                        else
                        {
                            reStr += "&nbsp;热";
                        }
                        break;
                    case 2:
                        if (value[i].ToString() == "1")
                        {
                            reStr += "&nbsp;<span style='color:red'>特</span>";
                        }
                        else
                        {
                            reStr += "&nbsp;特";
                        }
                        break;
                    case 3:
                        if (value[i].ToString() == "1")
                        {
                            reStr += "&nbsp;<span style='color:red'>活</span>";
                        }
                        else
                        {
                            reStr += "&nbsp;活";
                        }
                        break;
                    case 4:
                        if (value[i].ToString() == "1")
                        {
                            reStr += "&nbsp;<span style='color:red'>新</span>";
                        }
                        else
                        {
                            reStr += "&nbsp;新";
                        }
                        break;
                    case 6:
                        if (value[i].ToString() == "1")
                        {
                            reStr += "&nbsp;<span style='color:red'>精</span>";
                        }
                        else
                        {
                            reStr += "&nbsp;精";
                        }
                        break;
                    case 5:
                        if (value[i].ToString() == "1")
                        {
                            reStr += "&nbsp;<span style='color:red'>首</span>";
                        }
                        else
                        {
                            reStr += "&nbsp;首";
                        }
                        break;
                }

            }
            return reStr;
        }
        #endregion

        #region 标示
        protected string Designation(string str)
        {
            string reStr = string.Empty;
            string[] value = str.Split('|');
            for (int i = 0; i < value.Length; i++)
            {
                switch (i)
                {
                    case 0:
                        if (value[i].ToString() == "1")
                        {
                            reStr += "&nbsp;<span style='color:red'>普</span>";
                        }
                        else
                        {
                            reStr += "&nbsp;普";
                        }
                        break;
                    case 1:
                        if (value[i].ToString() == "1")
                        {
                            reStr += "&nbsp;<span style='color:red'>团</span>";
                        }
                        else
                        {
                            reStr += "&nbsp;团";
                        }
                        break;
                    case 2:
                        if (value[i].ToString() == "1")
                        {
                            reStr += "&nbsp;<span style='color:red'>拍</span>";
                        }
                        else
                        {
                            reStr += "&nbsp;拍";
                        }
                        break;
                    case 3:
                        if (value[i].ToString() == "1")
                        {
                            reStr += "&nbsp;<span style='color:red'>点</span>";
                        }
                        else
                        {
                            reStr += "&nbsp;点";
                        }
                        break;
                    case 4:
                        if (value[i].ToString() == "1")
                        {
                            reStr += "&nbsp;<span style='color:red'>批</span>";
                        }
                        else
                        {
                            reStr += "&nbsp;批";
                        }
                        break;
                }
            }
            return reStr;
        }
        #endregion

        #region
        private void del(string StrID)
        {
          
                YXShop.BLL.Product.ProductInfo bll = new YXShop.BLL.Product.ProductInfo();
                YXShop.BLL.Product.ProductAlbum pabll = new YXShop.BLL.Product.ProductAlbum();
                YXShop.BLL.Product.ProductSparepart sbll = new YXShop.BLL.Product.ProductSparepart();
                YXShop.BLL.Product.ProductSpecification spebll = new YXShop.BLL.Product.ProductSpecification();
                DataTable dt = bll.GetPartData(StrID);
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ChangeHope.Common.FileHelper fh = new ChangeHope.Common.FileHelper();
                        if (!dt.Rows[i]["pro_Thumbnail"].ToString().Contains("http://"))
                        {
                            fh.DeleteFile(Server.MapPath("~//" + dt.Rows[i]["pro_Thumbnail"].ToString()));
                        }
                        if (!dt.Rows[i]["pro_Images"].ToString().Contains("http://"))
                        {
                            fh.DeleteFile(Server.MapPath("~//" + dt.Rows[i]["pro_Images"].ToString()));
                        }
                        if (!dt.Rows[i]["pro_Original"].ToString().Contains("http://"))
                        {
                            fh.DeleteFile(Server.MapPath("~//" + dt.Rows[i]["pro_Original"].ToString()));
                        }
                        sbll.DeleteProductSparepart(Convert.ToInt32(dt.Rows[i]["pro_ID"].ToString()));
                        spebll.DeleteSpecificationProduct(Convert.ToInt32(dt.Rows[i]["pro_ID"].ToString()));
                    }
                }
                bll.Delete(StrID);
                pabll.DelAll(StrID);
                Response.Write("ok");
        }
        #endregion

        protected void Button1_Click(object sender, EventArgs e)
        {
            string PutoutType = this.w_d_pro_PutoutType.Value;
            GetList(PutoutType);
            this.txtArea.Text = string.Empty;
            this.txtProductClass.Text = string.Empty;
            this.txtUserName.Text = string.Empty;
            this.w_d_pro_CID.Value = "";
            this.w_l_pro_Area.Value = "";
            this.w_d_pro_PutoutID.Value = "";
        }

    }
}
